<template>
  <view class="container">
    <view class="evaluation-section">
      <text class="title">服务评价</text>

      <!-- 已评价提示 -->
      <view v-if="hasEvaluation" class="evaluation-tip">
        <text class="tip-text">您已对该订单进行评价，以下是您的评价内容：</text>
      </view>

      <!-- 打分项 -->
      <view class="rating-item">
  <text class="label">准时性：</text>
  <uni-rate v-model="punctualityRating" :max="4" :readonly="hasEvaluation" active-color="#FFD700" />
</view>
      <view class="rating-item">
        <text class="label">仪表：</text>
        <uni-rate v-model="appearanceRating" :max="4" :readonly="hasEvaluation" active-color="#FFD700" />
      </view>
      <view class="rating-item">
        <text class="label">态度：</text>
        <uni-rate v-model="attitudeRating" :max="4" :readonly="hasEvaluation" active-color="#FFD700" />
      </view>
      <view class="rating-item">
        <text class="label">责任心：</text>
        <uni-rate v-model="responsibilityRating" :max="4" :readonly="hasEvaluation" active-color="#FFD700" />
      </view>
      <view class="rating-item">
        <text class="label">专业性：</text>
        <uni-rate v-model="professionalRating" :max="4" :readonly="hasEvaluation" active-color="#FFD700" />
      </view>

      <!-- 输入框 -->
      <view class="input-item">
        <text class="label">不满意说明：</text>
        <uni-easyinput v-model="dissatisfactionReason" placeholder="请输入不满意说明（选填）" :disabled="hasEvaluation" />
      </view>
      <view class="input-item">
        <text class="label">综合评价：</text>
        <uni-easyinput v-model="overallEvaluation" placeholder="请输入综合评价（选填）" :disabled="hasEvaluation" />
      </view>
      <view class="input-item">
        <text class="label">改进意见：</text>
        <uni-easyinput v-model="improvementSuggestions" placeholder="请输入改进意见（选填）" :disabled="hasEvaluation" />
      </view>

      <!-- 提交按钮 -->
      <button class="submit-button" @click="submitEvaluation" v-if="!hasEvaluation">提交评价</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { orderrating, getOrderRating } from '@/api/system/login';

// 打分项
const punctualityRating = ref(0);
const appearanceRating = ref(0);
const attitudeRating = ref(0);
const responsibilityRating = ref(0);
const professionalRating = ref(0);

// 输入框
const comment = ref('');
const suggestion = ref('');
const other = ref('');
const dissatisfactionReason = ref('');
const overallEvaluation = ref('');
const improvementSuggestions = ref('');

// 订单ID
const orderId = ref('');
const caregiverId = ref('');
const hasEvaluation = ref(false);

onLoad(async (options) => {
  if (options.orderId) {
    orderId.value = options.orderId;
  }
  if (options.caregiverId) {
    caregiverId.value = options.caregiverId;
  }
  console.log('Received orderId:', orderId.value);
  console.log('Received caregiverId:', caregiverId.value);

  // 获取订单评价数据
  try {
    const res = await getOrderRating({ orderId: orderId.value });
    if (res.code === 200 && res.data.length > 0) {
      const evaluation = res.data[0]; // 假设每个订单只有一条评价
      hasEvaluation.value = true;

      // 回显评价内容
      punctualityRating.value = evaluation.punctualityRating;
      appearanceRating.value = evaluation.appearanceRating;
      attitudeRating.value = evaluation.attitudeRating;
      responsibilityRating.value = evaluation.responsibilityRating;
      professionalRating.value = evaluation.professionalRating;
      dissatisfactionReason.value = evaluation.dissatisfactionReason;
      overallEvaluation.value = evaluation.overallEvaluation;
      improvementSuggestions.value = evaluation.improvementSuggestions;
    }
  } catch (error) {
    console.error('获取评价数据失败:', error);
  }
});

// 提交评价
const submitEvaluation = async () => {
  try {
    const evaluationData = {
      punctualityRating: punctualityRating.value,
      appearanceRating: appearanceRating.value,
      attitudeRating: attitudeRating.value,
      responsibilityRating: responsibilityRating.value,
      professionalRating: professionalRating.value,
      comment: comment.value,
      suggestion: suggestion.value,
      other: other.value,
      dissatisfactionReason: dissatisfactionReason.value,
      overallEvaluation: overallEvaluation.value,
      improvementSuggestions: improvementSuggestions.value,
      orderId: orderId.value,
      caregiverId: caregiverId.value,
    };

    console.info('提交的评价数据:', JSON.parse(JSON.stringify(evaluationData)));

    const res = await orderrating(evaluationData);
    if (res.code === 200) {
      uni.showToast({
        title: '评价提交成功',
        icon: 'success',
      });
      setTimeout(() => {
        uni.navigateBack();
      }, 1500);
    } else {
      throw new Error(res.message || '评价提交失败');
    }
  } catch (error) {
    console.error('评价提交失败:', error);
    uni.showToast({
      title: error.message || '评价提交失败，请重试',
      icon: 'none',
    });
  }
};
</script>

<style scoped lang="scss">
.container {
  padding: 32rpx;
  background-color: #f8f8f8;
  min-height: 100vh;
}

.evaluation-section {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 24rpx;
}

.rating-item {
  display: flex;
  align-items: center;
  margin-bottom: 16rpx;

  .label {
    font-size: 28rpx;
    color: #333;
    margin-right: 16rpx;
  }
}

.input-item {
  margin-bottom: 16rpx;

  .label {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 8rpx;
  }
}

.submit-button {
  background-color: #ffcc00;
  color: #fff;
  font-size: 28rpx;
  font-weight: bold;
  border-radius: 8rpx;
  padding: 16rpx;
  margin-top: 24rpx;
}

.evaluation-tip {
  background-color: #fff;
  border-radius: 8rpx;
  padding: 16rpx;
  margin-bottom: 24rpx;
  text-align: center;

  .tip-text {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
  }
}

// 强制覆盖 uni-rate 的默认样式
.uni-rate {
  .uni-rate__icon {
    color: #FFD700 !important;
  }
}
</style> 